package com.sinog.biz.service.area.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sinog.biz.mapper.havexml.AreaMergeLogMapper;
import com.sinog.biz.mapper.havexml.DepartmentMapper;
import com.sinog.biz.mapper.havexml.SystemCodeMapper;
import com.sinog.biz.mapper.havexml.other.UserLoginDetailsMapper;
import com.sinog.biz.service.area.AreaMergeService;
import com.sinog.biz.service.common.CommonService;
import com.sinog.core.domain.AbAnzhiResultVo;
import com.sinog.core.domain.AbBangfuResultVo;
import com.sinog.core.domain.AbCunJiandingResultVo;
import com.sinog.core.domain.AbGengzongbangjiaoResultVo;
import com.sinog.core.domain.AbJiuResultVo;
import com.sinog.core.domain.AbLieguan;
import com.sinog.core.domain.AbMvResultVo;
import com.sinog.core.domain.AbPaichaResultVo;
import com.sinog.core.domain.AbPrisonersJcab;
import com.sinog.core.domain.AbPrisonersResultVo;
import com.sinog.core.domain.AbTuoguanResultVo;
import com.sinog.core.domain.AbZoufangResultVo;
import com.sinog.core.domain.AbZyzjiuzhuResultVo;
import com.sinog.core.domain.AreaMergeLog;
import com.sinog.core.domain.AzbjDatabaseInfo;
import com.sinog.core.domain.Department;
import com.sinog.core.domain.JypxJg;
import com.sinog.core.domain.Jytj;
import com.sinog.core.domain.Jyxxmk;
import com.sinog.core.domain.Log;
import com.sinog.core.domain.OrgStaffInfo;
import com.sinog.core.domain.PlacementBaseInfo;
import com.sinog.core.domain.PrisonerFx;
import com.sinog.core.domain.PrisonerHc;
import com.sinog.core.domain.PrisonerMain;
import com.sinog.core.domain.PrisonerSr;
import com.sinog.core.domain.PrisonerTs;
import com.sinog.core.domain.PrisonerXjxx;
import com.sinog.core.domain.PrisonerZn;
import com.sinog.core.domain.PrisonerZz;
import com.sinog.core.domain.SocialOrganizeInfo;
import com.sinog.core.domain.StaffInfoManage;
import com.sinog.core.domain.SystemCode;
import com.sinog.core.domain.User;
import com.sinog.core.domain.VolunteerManageInfo;
import com.sinog.core.domain.XjReceipt;
import com.sinog.core.domain.ZfbzXxgl;
import com.sinog.core.model.AuthUserVo;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.utilbean.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc 行政区划合并 - AreaMergeService
 * @author wangxj
 * @date 2022-11-08 11:12
 */
@Slf4j
@Service
public class AreaMergeServiceImpl implements AreaMergeService {

    @Resource
    private AreaMergeLogMapper areaMergeLogMapper;
    @Resource
    private SystemCodeMapper systemCodeMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private UserLoginDetailsMapper userLoginDetailsMapper;
    @Resource
    private CommonService commonService;

    /**
     * 根据uuid查找行政区划信息进行撤回
     * @param uuId uuid
     */
    @Override
    public void fallBackByUuid(String uuId) {
        List<AreaMergeLog> areaMergeLogs = areaMergeLogMapper.selectAreaMergeByUuid(uuId);
        UpdateWrapper<?> updateWrapper;
        for(AreaMergeLog areaMergeLog : areaMergeLogs) {
            String tableName = areaMergeLog.getTableName();
            String data = areaMergeLog.getData();
            String priNum = areaMergeLog.getPriNum();
            switch(tableName) {
                case "prisoner_main":
                    updateWrapper = new UpdateWrapper<PrisonerMain>();
                    updateWrapper.eq("pri_num",priNum);
                    PrisonerMain prisonerMain = JsonUtil.toBean(data,PrisonerMain.class);
                    prisonerMain.update((Wrapper<PrisonerMain>)updateWrapper);
                    break;
                case "ab_tuoguan":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbTuoguanResultVo abTuoguanResultVo = JsonUtil.toBean(data,AbTuoguanResultVo.class);
                    abTuoguanResultVo.update((Wrapper<AbTuoguanResultVo>)updateWrapper);
                    break;
                case "ab_zoufang":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbZoufangResultVo abZoufangResultVo = JsonUtil.toBean(data,AbZoufangResultVo.class);
                    abZoufangResultVo.update((Wrapper<AbZoufangResultVo>)updateWrapper);
                    break;
                case "ab_zyzjiuzhu":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbZyzjiuzhuResultVo abZyzjiuzhuResultVo = JsonUtil.toBean(data,AbZyzjiuzhuResultVo.class);
                    abZyzjiuzhuResultVo.update((Wrapper<AbZyzjiuzhuResultVo>)updateWrapper);
                    break;
                case "azbj_jdgl":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    PlacementBaseInfo placementBaseInfo = JsonUtil.toBean(data,PlacementBaseInfo.class);
                    placementBaseInfo.update((Wrapper<PlacementBaseInfo>)updateWrapper);
                    break;
                case "azbj_zlk":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AzbjDatabaseInfo azbjDatabaseInfo = JsonUtil.toBean(data,AzbjDatabaseInfo.class);
                    azbjDatabaseInfo.update((Wrapper<AzbjDatabaseInfo>)updateWrapper);
                    break;
                case "bjgz_ry":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    StaffInfoManage staffInfoManage = JsonUtil.toBean(data,StaffInfoManage.class);
                    staffInfoManage.update((Wrapper<StaffInfoManage>)updateWrapper);
                    break;
                case "jypx_jg":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    JypxJg jypxJg = JsonUtil.toBean(data,JypxJg.class);
                    jypxJg.update((Wrapper<JypxJg>)updateWrapper);
                    break;
                case "jyxx_mk":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    Jyxxmk jyxxmk = JsonUtil.toBean(data,Jyxxmk.class);
                    jyxxmk.update((Wrapper<Jyxxmk>)updateWrapper);
                    break;
                case "org_staff":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    OrgStaffInfo orgStaffInfo = JsonUtil.toBean(data,OrgStaffInfo.class);
                    orgStaffInfo.update((Wrapper<OrgStaffInfo>)updateWrapper);
                    break;
                case "prisoner_fx":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("pri_num",priNum);
                    PrisonerFx prisonerFx = JsonUtil.toBean(data,PrisonerFx.class);
                    prisonerFx.update((Wrapper<PrisonerFx>)updateWrapper);
                    break;
                case "prisoner_hc":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("pri_num",priNum);
                    PrisonerHc prisonerHc = JsonUtil.toBean(data,PrisonerHc.class);
                    prisonerHc.update((Wrapper<PrisonerHc>)updateWrapper);
                    break;
                case "prisoner_sr":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    PrisonerSr prisonerSr = JsonUtil.toBean(data,PrisonerSr.class);
                    prisonerSr.update((Wrapper<PrisonerSr>)updateWrapper);
                    break;
                case "prisoner_ts":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    PrisonerTs prisonerTs = JsonUtil.toBean(data,PrisonerTs.class);
                    prisonerTs.update((Wrapper<PrisonerTs>)updateWrapper);
                    break;
                case "prisoner_xjxx":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("pri_num",priNum);
                    PrisonerXjxx prisonerXjxx = JsonUtil.toBean(data,PrisonerXjxx.class);
                    prisonerXjxx.update((Wrapper<PrisonerXjxx>)updateWrapper);
                    break;
                case "prisoner_zn":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    PrisonerZn prisonerZn = JsonUtil.toBean(data,PrisonerZn.class);
                    prisonerZn.update((Wrapper<PrisonerZn>)updateWrapper);
                    break;
                case "prisoner_zz":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    PrisonerZz prisonerZz = JsonUtil.toBean(data,PrisonerZz.class);
                    prisonerZz.update((Wrapper<PrisonerZz>)updateWrapper);
                    break;
                case "shgyzz_xxgl":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    SocialOrganizeInfo socialOrganizeInfo = JsonUtil.toBean(data,SocialOrganizeInfo.class);
                    socialOrganizeInfo.update((Wrapper<SocialOrganizeInfo>)updateWrapper);
                    break;
                case "xj_receipt":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    XjReceipt xjReceipt = JsonUtil.toBean(data,XjReceipt.class);
                    xjReceipt.update((Wrapper<XjReceipt>)updateWrapper);
                    break;
                case "xtjc_jytj":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    Jytj jytj = JsonUtil.toBean(data,Jytj.class);
                    jytj.update((Wrapper<Jytj>)updateWrapper);
                    break;
                case "zfbz_xxgl":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    ZfbzXxgl zfbzXxgl = JsonUtil.toBean(data,ZfbzXxgl.class);
                    zfbzXxgl.update((Wrapper<ZfbzXxgl>)updateWrapper);
                    break;
                case "zyz_xxgl":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    VolunteerManageInfo volunteerManageInfo = JsonUtil.toBean(data,VolunteerManageInfo.class);
                    volunteerManageInfo.update((Wrapper<VolunteerManageInfo>)updateWrapper);
                    break;
                case "ab_anzhi":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbAnzhiResultVo anzhiResultVo = JsonUtil.toBean(data,AbAnzhiResultVo.class);
                    anzhiResultVo.update((Wrapper<AbAnzhiResultVo>)updateWrapper);
                    break;
                case "ab_bangfu":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbBangfuResultVo abBangfuResultVo = JsonUtil.toBean(data,AbBangfuResultVo.class);
                    abBangfuResultVo.update((Wrapper<AbBangfuResultVo>)updateWrapper);
                    break;
                case "ab_cun_jianding":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbCunJiandingResultVo abCunJiandingResultVo = JsonUtil.toBean(data,AbCunJiandingResultVo.class);
                    abCunJiandingResultVo.update((Wrapper<AbCunJiandingResultVo>)updateWrapper);
                    break;
                case "ab_genzongbangjiao":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbGengzongbangjiaoResultVo gengzongbangjiaoResultVo = JsonUtil.toBean(data,AbGengzongbangjiaoResultVo.class);
                    gengzongbangjiaoResultVo.update((Wrapper<AbGengzongbangjiaoResultVo>)updateWrapper);
                    break;
                case "ab_jiu":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbJiuResultVo abJiuResultVo = JsonUtil.toBean(data,AbJiuResultVo.class);
                    abJiuResultVo.update((Wrapper<AbJiuResultVo>)updateWrapper);
                    break;
                case "ab_lieguan":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("pri_num",priNum);
                    AbLieguan abLieguan = JsonUtil.toBean(data,AbLieguan.class);
                    abLieguan.update((Wrapper<AbLieguan>)updateWrapper);
                    break;
                case "ab_mv":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("pri_num",priNum);
                    AbMvResultVo abMvResultVo = JsonUtil.toBean(data,AbMvResultVo.class);
                    abMvResultVo.update((Wrapper<AbMvResultVo>)updateWrapper);
                    break;
                case "ab_paicha":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    AbPaichaResultVo abPaichaResultVo = JsonUtil.toBean(data,AbPaichaResultVo.class);
                    abPaichaResultVo.update((Wrapper<AbPaichaResultVo>)updateWrapper);
                    break;
                case "ab_prisoners":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("pri_num",priNum);
                    AbPrisonersResultVo abPrisonersResultVo = JsonUtil.toBean(data,AbPrisonersResultVo.class);
                    abPrisonersResultVo.update((Wrapper<AbPrisonersResultVo>)updateWrapper);
                    break;
                case "ab_prisoners_jcab":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("pri_num",priNum);
                    AbPrisonersJcab abPrisonersJcab = JsonUtil.toBean(data,AbPrisonersJcab.class);
                    abPrisonersJcab.update((Wrapper<AbPrisonersJcab>)updateWrapper);
                    break;
                case "system_log":
                    updateWrapper = new UpdateWrapper<>();
                    updateWrapper.eq("id",priNum);
                    Log log = JsonUtil.toBean(data,Log.class);
                    log.update((Wrapper<Log>)updateWrapper);
                    break;
                // 如果存在记录可进行修改撤回,撤回存在风险,可能经历多次处理后直接退回到第一次处理的时候(账号有点危险)
                case "tbsys_code":
                    // 1.判断数据库中是否能查找记录
                    SystemCode systemCode = JsonUtil.toBean(data,SystemCode.class);
                    String codeId = systemCode.getCodeId();
                    SystemCode systemCode1 = systemCodeMapper.getByCode(Constants.CodeType.CODE_ZHRMGHG_XZQH_DM,codeId);
                    if(null != systemCode1) {
                        Map<String,String> codeMap = new HashMap<>(16);
                        codeMap.put("codeId",codeId);
                        systemCodeMapper.updateSystemCode(systemCode,codeMap);
                    } else {
                        systemCode.insert();
                    }
                    break;
                case "tbsys_department":
                    Department department = JsonUtil.toBean(data,Department.class);
                    String id = department.getId();
                    Department department1 = departmentMapper.getDepartIdByNativevill(id);
                    if(null != department1) {
                        Map<String,String> departmentMap = new HashMap<>(16);
                        departmentMap.put("id",id);
                        departmentMapper.updateDepartmentById(department,departmentMap);
                    } else {
                        department.insert();
                    }
                    break;
                case "tbsys_user":
                    User user = JsonUtil.toBean(data,User.class);
                    String account = user.getAccount();
                    User user1 = userLoginDetailsMapper.getUserByAccount(account);
                    if(null != user1) {
                        userLoginDetailsMapper.updateSystemUser(user);
                    } else {
                        userLoginDetailsMapper.insertSystemUser(user);
                    }
                    break;
                case "auth_user":
                    AuthUserVo authUserVo = JsonUtil.toBean(data,AuthUserVo.class);
                    String account1 = authUserVo.getAccount();
                    AuthUserVo authUser = userLoginDetailsMapper.selectAuthUserByAccount(account1);
                    if(null != authUser) {
                        userLoginDetailsMapper.updateAuthUser(authUserVo);
                    } else {
                        userLoginDetailsMapper.insertAuthUser(authUserVo);
                    }
                    break;
                default:
                    break;
            }
        }
        commonService.executeDepartChild();
    }
}